<?php

namespace common\services\cms;

use common\services\BaseService;
use common\repositories\cms\ArticleRepository;
use common\repositories\cms\DirRepository;
use common\entities\cms\ArticleEntity;

class ArticleService extends BaseService {

    public $_ArticleRepository;
    public $_DirRepository;
    
    public function __construct(ArticleRepository $ArticleRepository, DirRepository $DirRepository) {
        $this->_ArticleRepository = $ArticleRepository;
        $this->_DirRepository = $DirRepository;
    }
    
    /**
     * 更新文章点击数
     * @param type $articleId
     */
    public function updatePv ($id) {
        if ( ! empty($id)) {
            $this->_ArticleRepository->updatePv($id);
        }
    }
    
    public function updateShareNum($id) {
        if ( ! empty($id)) {
            $this->_ArticleRepository->updateShareNum($id);
        }        
    }
    
    public function saveArticle ($model) {
        if (isset($model->aId) && !empty($model->aId)) {
            //修改
            $result = $this->_ArticleRepository->updateArticle($model);
        } else {
            //新增
            $result = $this->_ArticleRepository->addArticle($model);
        }
        return $result;
    }
    
    public function getArticleInfo ($aId) {
        $list = $this->_ArticleRepository->getArticleInfo($aId);
        if(empty($aId)) {
            $list = array(
                'title' => '',
                'tag' => '',
                'pv' => '0',
                'image_url' => '',
                'file_url' => '',
                'file_name' => '',
                'file_url2' => '',
                'file_name2' => '',
                'file_url3' => '',
                'file_name3' => '',
                'a_id' => '',
                'content' => '',
                'dir_id' => ''
            );
        } else {
            $urlList = explode("@@", $list['file_url']);
            $list['file_url'] = $urlList[0];
            $list['file_url2'] = isset($urlList[1]) ? $urlList[1] : "";
            $list['file_url3'] = isset($urlList[2]) ? $urlList[2] : "";
            $nameList = explode("@@", $list['file_name']);
            $list['file_name'] = $nameList[0];
            $list['file_name2'] = isset($nameList[1]) ? $nameList[1] : "";
            $list['file_name3'] = isset($nameList[2]) ? $nameList[2] : "";
        }
        return $list;
        //$dirList = $this->_DirRepository->getDirList();
    }

    public function getArticleAllList ($sidx, $sord, $page, $page_size, $search) {
        return $this->_ArticleRepository->getArticleAllList($sidx, $sord, $page, $page_size, $search);
    }
    
    public function getArticleList($dirId,$limit) {
        return $this->_ArticleRepository->getArticleListByDirId($dirId,$limit);
    }
    
    public function getArticleListByKeyword ($keyword, $offset=0, $limit=10) {
        $query = ArticleEntity::find();
        if (! empty($keyword)) {
            $query = $query->where(['like', 'title', $keyword])
                ->orwhere(['like', 'tag', $keyword]);
        }  
        $query = $query->andwhere(['del' => 0])
            ->andwhere(['status' => 1])
            ->offset((int)$offset*$limit)
            ->limit((int)$limit)
            ->all();  
        return $query;   
    }
    
    public function getArticleCountByKeyword ($keyword) {
        $query = ArticleEntity::find();
        if (! empty($keyword)) {
            $query = $query->where(['like', 'title', $keyword])
                ->orwhere(['like', 'tag', $keyword]);
        }  
        $query = $query->andwhere(['del' => 0]) 
            ->andwhere(['status' => 1])
            ->count();  
        return $query;   
    }   
    
    /**
     * 批量修改
     * @param type $section
     * @param type $value
     * @param type $ids
     */
    public function updateBatch ($section, $value, $str) {
        $ids = explode(':', $str);
        $idArr = [];
        foreach ($ids as $val) {
            is_numeric($val) && $idArr[] = $val;
        }
        return $this->_ArticleRepository->updateBatch($section, $value, $idArr);
    }
    
    /**
     * 字段处理
     */
    public function dealField ($field) {
        $field == 'id' && $field = 'a_id';
        $field == 'dirName' && $field = 'dir_id';
        $field == 'modifiedTime' && $field = 'modified_time';
        $field == 'modifiedBy' && $field = 'modified_by';
        $field == 'edit' && $field = 'a_id';  
        return $field;
    }
}